﻿//        Another Demo from Andy L. & MissedMemo.com
// Borrow whatever code seems useful - just don't try to hold
// me responsible for any ill effects. My demos sometimes use
// licensed images which CANNOT legally be copied and reused.

using System;
using System.Linq;
using System.Xml.Linq;
using System.Collections.Generic;


namespace MissedMemo.MeatAndCheeseDisc.OrderEntry
{
    public static class LookupMenuItem
    {
        public readonly static string strMenuFileName = "Menu.xml";


        public static IEnumerable<string> GetTypes()
        {
            XElement data = XElement.Load( strMenuFileName );

            return
                from itemType in data.Descendants( "Pizza" )
                select (String)itemType.Attribute( "DisplayName" );
        }


        public static IEnumerable<string> GetSizes()
        {
            XElement data = XElement.Load( strMenuFileName );

            var query =
                from itemSize in data.Descendants( "Size" )
                select (String)itemSize.Attribute( "DisplayName" );

            return query.Distinct();
        }


        public static double GetPrice( string strItemName, string strItemSize )
        {
            XElement data = XElement.Load( strMenuFileName );

            var query =
                from itemType in data.Descendants()
                where (String)itemType.Attribute( "DisplayName" ) == strItemName
                from itemSize in itemType.Descendants( "Size" )
                where (String)itemSize.Attribute( "DisplayName" ) == strItemSize
                select itemSize.Descendants( "Price" ).FirstOrDefault();

            double price = 0;

            if( query.ElementAt( 0 ) != null )
                Double.TryParse( query.ElementAt( 0 ).Value, out price );

            return price;
        }
    }
}
